package com.wcgksoft.controller.admin;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.goods.Coupon;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.admin.AccountService;
import com.wcgksoft.service.system.UserService;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.controller.admin
 * @Description TODO admin下用户管理
 * @date 2020/5/18 9:05
 * @ClassName AdminController
 */
@Controller
@RequestMapping("adminUser")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private AccountService accountService;

    /**
     * 用户列表
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "page",required = false)Integer page,
                         @RequestParam(value = "limit",required = false)Integer limit,
                         @RequestParam(value = "name",required = false)String keyWord){
        DetachedCriteria criteria = DetachedCriteria.forClass(SysUser.class);
        if (StringUtils.isNotEmpty(keyWord)){
            criteria.add(Restrictions.or(Restrictions.like("name","%"+keyWord+"%", MatchMode.ANYWHERE),
                    Restrictions.like("mobile","%"+keyWord+"%", MatchMode.ANYWHERE),
                    Restrictions.like("address","%"+keyWord+"%", MatchMode.ANYWHERE)));
        }
        criteria.add(Restrictions.eq("status",1));
        criteria.addOrder(Order.desc("createDate"));
        List<SysUser> list = userService.findByConditions(criteria, page, limit);
        return Layui.data(0, "success", userService.getTotalCount(), list);
    }


    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id",required = false) Integer id){
        try {
            SysUser sysUser = userService.findById(id);
            sysUser.setStatus(0);
            userService.saveMerge(sysUser);
            return Layui.data(0,"操作成功",0,null);
        }catch (Exception e){
            e.printStackTrace();
            return Layui.data(0,"删除失败",0,null);
        }

    }


}
